package eabnp.eflow.server.impl.hb.imptool;

import eabnp.basic.exception.EABnPRuntimeException;
import eabnp.basic.property.EPropertyMap;
import eabnp.basic.result.GResult;
import eabnp.basic.result.IResult;
import eabnp.basic.result.SGResult;
import eabnp.basic.util.DateTimeUtil;
import eabnp.basic.util.StringUtil;
import eabnp.basic.util.array.ArrayUtil;
import eabnp.eflow.EFlow;
import eabnp.eflow.EFlowConst;
import eabnp.eflow.api.AppInterfaceType;
import eabnp.eflow.info.ctrlinfo.sysctrl.basicinfo.AppInfo;
import eabnp.eflow.info.ctrlinfo.sysctrl.basicinfo.SysInfo;
import eabnp.eflow.info.ctrlinfo.sysctrl.condition.ConditionInfo;
import eabnp.eflow.info.ctrlinfo.sysctrl.condition.ConditionTool;
import eabnp.eflow.info.ctrlinfo.sysctrl.condition.UserScopeInfo;
import eabnp.eflow.misc.UserActionType;
import eabnp.eflow.misc.acttype.ComActType;
import eabnp.eflow.misc.acttype.FlowActType;
import eabnp.eflow.misc.noneusergroup.GroupElementList;
import eabnp.eflow.misc.noneusergroup.NoneUserGroupTool;
import eabnp.eflow.misc.usualcomment.UserUsualWordComment;
import eabnp.eflow.server.impl.hb.EFlowHBUtil;
import eabnp.eflow.server.impl.hb.EFlowServerByHB;
import eabnp.eflow.server.impl.hb.cache.EFlowCache;
import eabnp.eflow.server.impl.hb.cache.FlowInfoCache;
import eabnp.eflow.server.impl.hb.cache.FlowVersionCache;
import eabnp.eflow.server.impl.hb.orm.AbsAppChildObj;
import eabnp.eflow.server.impl.hb.orm.HAction;
import eabnp.eflow.server.impl.hb.orm.HApp;
import eabnp.eflow.server.impl.hb.orm.HAppObj;
import eabnp.eflow.server.impl.hb.orm.HCommentGroup;
import eabnp.eflow.server.impl.hb.orm.HFlow;
import eabnp.eflow.server.impl.hb.orm.HFlowGroup;
import eabnp.eflow.server.impl.hb.orm.HFlowVersion;
import eabnp.eflow.server.impl.hb.orm.HForm;
import eabnp.eflow.server.impl.hb.orm.HFun;
import eabnp.eflow.server.impl.hb.orm.HFunGroup;
import eabnp.eflow.server.impl.hb.orm.HGroup;
import eabnp.eflow.server.impl.hb.orm.HOAServer;
import eabnp.eflow.server.impl.hb.orm.HObjProperty;
import eabnp.eflow.server.impl.hb.orm.HOptionGroup;
import eabnp.eflow.server.impl.hb.orm.HOrg;
import eabnp.eflow.server.impl.hb.orm.HOrgUnit;
import eabnp.eflow.server.impl.hb.orm.HResource;
import eabnp.eflow.server.impl.hb.orm.HSys;
import eabnp.eflow.server.impl.hb.orm.HViewOAServer;
import eabnp.eflow.server.impl.hb.orm.HViewOrg;
import eabnp.eflow.server.impl.hb.orm.HViewOrgUnit;
import eabnp.eflow.server.impl.hb.orm.HViewUser;
import eabnp.eflow.server.impl.notes.DominoServerUtil;
import eabnp.eflow.session.ESession;
import eabnp.eflow.tag.EFlowTagType;
import eabnp.eflow.tag.EFlowTagUtil;
import eabnp.eoperation.IEOperation;
import eabnp.etag.ETagUtil;
import eabnp.pfcenter.PFCenter;
import eabnp.pfcenter.PFCenterUtil;
import eabnp.service.hbcenter.HBCenter;
import eabnp.service.hbcenter.HBSessionWrapper;
import eabnp.service.hbcenter.HBUtil;
import eabnp.service.hbcenter.bclob.IBClobHandle;
import eabnp.service.idcenter.IdCenter;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

public final class OperateCreate
{
  private static final String UNIQUE_NAME = "UniqueName";

  private static IResult create(ESession es, HBSessionWrapper sessionWrapper, IEOperation operation, Object obj, EPropertyMap pMap)
  {
    String errMsg = "创建失败";

    if (EFlowTagType.O == obj) {
      return createOrg(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.APP == obj) {
      return createApp(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.OU == obj) {
      return createOrgUnit(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.RES == obj) {
      return createResource(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.FUN == obj) {
      return createFun(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.GROUP == obj) {
      return createGroup(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.FUNGROUP == obj) {
      return createFunGroup(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.SYS == obj) {
      return createSys(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.USER == obj) {
      return createUser(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.PUSER == obj) {
      return createPUser(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.FLOW == obj) {
      return createFlow(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.FORM == obj) {
      return createForm(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.ACT == obj) {
      return createAction(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.COMMENTGROUP == obj) {
      return createCommentGroup(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.COMMENTCOLUMN == obj) {
      return createCommentColumn(es, sessionWrapper, operation, pMap);
    }
    if (EFlowTagType.APPOBJ == obj) {
      return createAppObj(es, sessionWrapper, operation, pMap);
    }

    if (EFlowTagType.OBJPROPERTY == obj) {
      return createObjProperty(es, sessionWrapper, operation, pMap);
    }

    if (EFlowTagType.PRINTTEMPLATE == obj) {
      return createPrintTemplate(es, sessionWrapper, operation, pMap);
    }

    if (EFlowTagType.OPTIONGROUP == obj) {
      return createOptionGroup(es, sessionWrapper, operation, pMap);
    }

    if (EFlowTagType.FLOWVERSION == obj) {
      return createFlowVersion(es, sessionWrapper, operation, pMap);
    }

    return SGResult.newFailureResult("不支持的对象类型").addErrMsgPreStr(errMsg);
  }

  public static IResult create(ESession es, IEOperation operation)
  {
    String errMsg = "创建失败";

    Object obj = operation.getParameter("objectType");
    if (obj == null) {
      return SGResult.newFailureResult("空对象类型").addErrMsgPreStr(errMsg);
    }

    EPropertyMap pMap = operation
      .getParamAsPropertyMap("propertyMap");
    if (pMap == null) {
      return SGResult.newFailureResult("空的属性列表").addErrMsgPreStr(errMsg);
    }

    if (obj != EFlowTagType.FLOWVERSION) {
      String code = pMap.getPropertyString("code");
      if (StringUtil.isNullOrEmptyStr(code)) {
        if (obj == EFlowTagType.APP)
          errMsg = "创建" + ((EFlowTagType)obj).getDescription() + "失败";
        else if ((obj != EFlowTagType.USER) && (obj != EFlowTagType.PUSER))
          return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
      }
      else errMsg = "创建" + ((EFlowTagType)obj).getDescription() + "〖" + code + 
          "〗失败";
    }

    GResult gr = EFlowHBUtil.openHBSession();
    if (gr.isFailure()) {
      gr.addErrMsgPreStr(errMsg);
      return gr;
    }
    Session session = (Session)gr.getResultObject();
    HBSessionWrapper sessionWrapper = new HBSessionWrapper(session, 
      EFlow.getInstance().getMyLogger());
    IResult r = null;
    try {
      r = create(es, sessionWrapper, operation, obj, pMap);
    } catch (Exception ex) {
      r = GResult.newFailureResult(errMsg, ex);
    }

    sessionWrapper.close();
    return r;
  }

  private static IResult createFlowVersion(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建" + EFlowTagType.FLOWVERSION.getDescription() + "失败";

    HFlowVersion newFV = new HFlowVersion();
    newFV
      .setDescription(pMap.getPropertyString("desc"));

    String flowUN = pMap.getPropertyString("un");
    if (StringUtil.isNullOrEmptyStr(flowUN)) {
      return SGResult.newFailureResult("流程组合代码为空").addErrMsgToIResult(errMsg);
    }
    errMsg = "创建流程[" + flowUN + "]的新版本失败";

    GResult gr = GResult.newSuccessResult();
    Session session = sessionWrapper.getSession();
    Object obj = EFlowHBUtil.queryObjByUN(session, 
      EFlowHBUtil.getClassName(EFlowTagType.FLOW), flowUN);
    if (obj == null)
      return gr.resetFailureResult("没有找到流程").addErrMsgPreStr(errMsg);
    HFlow flow = (HFlow)obj;
    newFV.setFlow(flow);

    Set set = flow.getVersionSet();
    int flowVersion = 1;
    if (set.size() > 0) {
      HFlowVersion maxFV = (HFlowVersion)set.iterator().next();
      flowVersion = maxFV.getVersionInt() + 1;
    }
    newFV.setVersionInt(flowVersion);
    newFV.setCreateTime(DateTimeUtil.getNow());

    String mainFormUN = pMap
      .getPropertyString("mainFormUN");
    if (StringUtil.isNotNullEmptyStr(mainFormUN)) {
      obj = EFlowHBUtil.queryIdByUN(session, EFlowTagType.FLOW, mainFormUN);
      if (obj == null) {
        return gr.resetFailureResult("没有找到主表单").addErrMsgPreStr(errMsg);
      }

    }

    String mainCGUN = pMap.getPropertyString("mainCGUN");
    if (StringUtil.isNotNullEmptyStr(mainCGUN)) {
      obj = EFlowHBUtil.queryObjByUN(session, EFlowTagType.COMMENTGROUP, 
        mainCGUN);
      if (obj == null) {
        return gr.resetFailureResult("没有找到主意见组").addErrMsgPreStr(errMsg);
      }
      newFV.setMainCommentGroup((HCommentGroup)gr.getResultObject());
    }

    if (!sessionWrapper.beginTransaction())
      return gr.resetFailureResult("开始数据库事务失败");
    newFV.setClob(Hibernate.createClob(""));
    session.save(newFV);
    sessionWrapper.commit();

    FlowInfoCache fCache = EFlowCache.getFlowCache(flowUN);
    if (fCache != null) {
      FlowVersionCache fvCache = new FlowVersionCache();
      fCache.putFlowVersionCache(newFV
        .resetFlowVersionCache(fvCache, false, 
        null));
    }

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString(
      "version", String.valueOf(flowVersion)));
  }

  private static IResult createObjProperty(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建应用对象属性失败";

    HObjProperty newObjProperty = new HObjProperty();
    EFlowHBUtil.setBasicProperty(newObjProperty, pMap);
    if (pMap.hasProperty("dataType")) {
      newObjProperty.setDataTypeInt(
        pMap.getPropertyInt("dataType"));
    }

    String code = newObjProperty.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newObjProperty.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType != EFlowTagType.APPOBJ) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    errMsg = "创建应用对象属性" + code + "(" + name + ")失败";

    Session session = sessionWrapper.getSession();
    GResult gr = GResult.newSuccessResult();

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, pType, pun);
    if (gr.isFailure()) {
      return gr.addErrMsgPreStr("查询父节点失败：" + pun).addErrMsgPreStr(errMsg);
    }

    if (gr.isNullResult()) {
      return gr.resetFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    }

    HAppObj appObj = (HAppObj)gr.getResultObject();

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(
      EFlowHBUtil.getClassName(EFlowTagType.OBJPROPERTY));
    sb.append(" as obj where obj.appObj=:appObj");
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setEntity("appObj", appObj);
      q.setString("code", code);
      q.setString("name", name);
      newObj = q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (newObj != null) {
      return gr.resetFailureResult("代码冲突").addErrMsgPreStr(errMsg);
    }

    newObjProperty.setAppObj(appObj);
    newObjProperty.resetUniqueName();

    gr = HBUtil.saveObject_CacheEx(session, newObjProperty);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr(errMsg);
      sessionWrapper.setNeedRollBack(true);
    }

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newObjProperty.getUniqueName()));
  }

  private static IResult createAppChild(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EFlowTagType type, EPropertyMap pMap)
  {
    String errMsg = "创建" + type.getDescription() + "失败";

    AbsAppChildObj newAC = AbsAppChildObj.newAppChild(type);
    EFlowHBUtil.setBasicProperty(newAC, pMap);

    String code = newAC.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newAC.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    Session session = sessionWrapper.getSession();

    if (type == EFlowTagType.ACT) {
      HAction act = (HAction)newAC;

      String script = pMap.getPropertyString("script");
      act.setScript(script);

      String vScript = pMap
        .getPropertyString("vScript");
      act.setVscript(vScript);

      String tip = pMap.getPropertyString("tip");
      act.setTip(tip);

      if (pMap.hasProperty("isFlowAct")) {
        act.setFlowActFlag(
          pMap.getPropertyBool("isFlowAct"));
      }
      if (pMap.hasProperty("flowActType")) {
        act.setFlowActType(FlowActType.valueOf(
          pMap.getPropertyString("flowActType")));
      }
      if (pMap.hasProperty("actType")) {
        act.setActType(ComActType.valueOf(
          pMap.getPropertyInt("actType")));
      }

      String style = pMap.getPropertyString("style");
      act.setStyle(style);

      String cs = pMap.getPropertyString("condition");
      ConditionInfo cInfo = ConditionTool.genConditionByFormattedString(cs);
      act.setDefaultConditon(session, cInfo);
    }
    else if (type == EFlowTagType.OPTIONGROUP) {
      String options = pMap.getPropertyString("options");
      ((HOptionGroup)newAC).setOptions(options);
    }
    else if (type == EFlowTagType.FLOW) {
      HFlow flow = (HFlow)newAC;
      if (pMap.hasProperty("deptLevel")) {
        int l = pMap.getPropertyInt("deptLevel");
        flow.setDepartmentLevel(EFlow.getProperDepartmentLevel(l));
      }
      String mainFormUN = pMap
        .getPropertyString("mainFormUN");
      String todoLabel = pMap
        .getPropertyString("todoLabel");
      flow.setTodoLabel(todoLabel);
      if (StringUtil.isNotNullEmptyStr(mainFormUN)) {
        GResult gr = EFlowHBUtil.queryIdByUN_CatchEx(session, 
          EFlowTagType.FLOW, mainFormUN);
        if (gr.isFailure()) {
          return gr.addErrMsgPreStr("查找主表单失败").addErrMsgPreStr(errMsg);
        }
        if (gr.isNullResult()) {
          return gr.resetFailureResult("没有找到主表单").addErrMsgPreStr(errMsg);
        }
        flow.setMainForm((HForm)gr.getResultObject());
      }

      String mainCGUN = pMap.getPropertyString("mainCGUN");
      if (StringUtil.isNotNullEmptyStr(mainCGUN)) {
        GResult gr = EFlowHBUtil.queryObjByUN_CatchEx(session, 
          EFlowTagType.COMMENTGROUP, mainCGUN);
        if (gr.isFailure()) {
          return gr.addErrMsgPreStr("查找主意见组失败").addErrMsgPreStr(errMsg);
        }
        if (gr.isNullResult()) {
          return gr.resetFailureResult("没有找到主意见组").addErrMsgPreStr(errMsg);
        }
        flow.setMainCommentGroup((HCommentGroup)gr.getResultObject());
      }
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(type)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建角色")
        .addErrMsgPreStr(errMsg);
    }

    errMsg = "在" + pun + "下创建" + type.getDescription() + code + "(" + name + 
      ")失败";

    GResult gr = GResult.newSuccessResult();

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, 
      EFlowHBUtil.getClassName(pType), pun);
    if (gr.isFailure()) {
      return gr.addErrMsgPreStr("查询父节点失败").addErrMsgPreStr(errMsg);
    }

    if (gr.isNullResult()) {
      return gr.resetFailureResult("没有找到父节点").addErrMsgPreStr(errMsg);
    }

    HApp app = (HApp)gr.getResultObject();

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(type));
    sb.append(" as obj where obj.app=:app");
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setEntity("app", app);
      q.setString("code", code);
      q.setString("name", name);
      newObj = q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (newObj != null) {
      return gr.resetFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    List<HGroup> groupList = null;
    List<HGroup> deptGroupList = null;
    String[] sa;
    if (type == EFlowTagType.FLOW) {
      HFlow flow = (HFlow)newAC;
      boolean bool = pMap.getPropertyBool("subFlowFlag");
      flow.setSubflow(bool);

      String readerGroup = pMap
        .getPropertyString("readerGroup");
      if ((!flow.isSubflow()) && (StringUtil.isNotNullEmptyStr(readerGroup))) {
        sa = StringUtil.split(readerGroup, ";");
        int n = sa.length;
        groupList = new ArrayList<HGroup>();
        for (int i = 0; i < n; i++) {
          HGroup group = (HGroup)EFlowHBUtil.queryObjByUN(session, 
            EFlowTagType.GROUP, sa[i]);
          if (group == null) {
            return gr.resetFailureResult("无法找到群组[" + sa[i] + "]");
          }
          groupList.add(group);
        }
      }

      readerGroup = pMap
        .getPropertyString("deptReaderGroup");
      if ((!flow.isSubflow()) && (StringUtil.isNotNullEmptyStr(readerGroup))) {
        sa = StringUtil.split(readerGroup, 
          ";");
        int n = sa.length;
        deptGroupList = new ArrayList();
        for (int i = 0; i < n; i++) {
          HGroup group = (HGroup)EFlowHBUtil.queryObjByUN(session, 
            EFlowTagType.GROUP, sa[i]);
          if (group == null) {
            return gr.resetFailureResult("无法找到群组[" + sa[i] + "]");
          }
          deptGroupList.add(group);
        }
      }
    }

    newAC.setApp(app);
    newAC.resetUniqueName();

    if (!sessionWrapper.beginTransaction())
      return gr.resetFailureResult("开始数据库事务失败");
    session.save(newAC);

    if (type == EFlowTagType.FLOW)
    {
      HFlow flow = (HFlow)newAC;
      HFlowVersion fv = new HFlowVersion();
      fv.setFlow(flow);
      fv.setCreateTime(DateTimeUtil.getNow());
      fv.setVersionInt(1);
      session.save(fv);

      if ((groupList != null) || (deptGroupList != null)) {
        if (groupList != null) {
          for (HGroup g : groupList) {
            HFlowGroup fg = new HFlowGroup();
            fg.setFlow(flow);
            fg.setGroup(g);
            fg.setDeptFlag(false);
            session.save(fg);
          }
        }

        if (deptGroupList != null) {
          for (HGroup g : deptGroupList) {
            HFlowGroup fg = new HFlowGroup();
            fg.setFlow(flow);
            fg.setGroup(g);
            fg.setDeptFlag(true);
            session.save(fg);
          }
        }
      }
    }
    sessionWrapper.commit();

    if (type == EFlowTagType.ACT) {
      EFlowCache.putActInfo(((HAction)newAC).toBasicInfo());
    } else if ((type == EFlowTagType.FLOW) || (type == EFlowTagType.SUBFLOW)) {
      session.refresh(newAC);
      EFlowCache.putFlowCache(EFlowServerByHB.genFlowCache((HFlow)newAC));
    }

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newAC.getUniqueName()));
  }

  private static IResult createAppObj(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    return createAppChild(es, sessionWrapper, op, EFlowTagType.APPOBJ, pMap);
  }

  private static IResult createCommentColumn(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    return createAppChild(es, sessionWrapper, op, EFlowTagType.COMMENTCOLUMN, 
      pMap);
  }

  private static IResult createCommentGroup(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    return createAppChild(es, sessionWrapper, op, EFlowTagType.COMMENTGROUP, 
      pMap);
  }

  private static IResult createOptionGroup(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    return createAppChild(es, sessionWrapper, op, EFlowTagType.OPTIONGROUP, 
      pMap);
  }

  private static IResult createPrintTemplate(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    return createAppChild(es, sessionWrapper, op, EFlowTagType.PRINTTEMPLATE, 
      pMap);
  }

  private static IResult createAction(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    EFlowTagType type = EFlowTagType.ACT;
    String errMsg = "创建" + EFlowTagType.ACT.getDescription() + "失败";

    HAction newAct = new HAction();
    EFlowHBUtil.setBasicProperty(newAct, pMap);

    String code = newAct.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newAct.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    Session session = sessionWrapper.getSession();

    String script = pMap.getPropertyString("script");
    newAct.setScript(script);

    String vScript = pMap.getPropertyString("vScript");
    newAct.setVscript(vScript);

    String tip = pMap.getPropertyString("tip");
    newAct.setTip(tip);

    if (pMap.hasProperty("isFlowAct")) {
      newAct.setFlowActFlag(
        pMap.getPropertyBool("isFlowAct"));
    }
    if (pMap.hasProperty("flowActType")) {
      newAct.setFlowActType(FlowActType.valueOf(
        pMap.getPropertyString("flowActType")));
    }
    if (pMap.hasProperty("actType")) {
      newAct.setActType(ComActType.valueOf(
        pMap.getPropertyInt("actType")));
    }

    String style = pMap.getPropertyString("style");
    newAct.setStyle(style);

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(type)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建角色")
        .addErrMsgPreStr(errMsg);
    }

    errMsg = "在" + pun + "下创建" + type.getDescription() + code + "(" + name + 
      ")失败";

    Object obj = EFlowHBUtil.queryObjByUN(session, 
      EFlowHBUtil.getClassName(pType), pun);
    if (obj == null) {
      return GResult.newFailureResult("没有找到父节点").addErrMsgPreStr(errMsg);
    }
    HApp app = (HApp)obj;

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(type));
    sb.append(" as obj where obj.app=:app");
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    Query q = session.createQuery(sb.toString());
    q.setEntity("app", app);
    q.setString("code", code);
    q.setString("name", name);
    q.setMaxResults(1);
    newObj = q.uniqueResult();
    if (newObj != null) {
      return GResult.newFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    newAct.setApp(app);
    newAct.resetUniqueName();

    if (!sessionWrapper.beginTransaction())
      return GResult.newFailureResult("开始数据库事务失败");
    String cs = pMap.getPropertyString("condition");
    ConditionInfo cInfo = ConditionTool.genConditionByFormattedString(cs);
    if (cInfo != null) newAct.setDefaultConditon(session, cInfo);

    session.save(newAct);
    sessionWrapper.commit();

    EFlowCache.putActInfo(newAct.toBasicInfo());

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newAct.getUniqueName()));
  }

  private static IResult createForm(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    return createAppChild(es, sessionWrapper, op, EFlowTagType.FORM, pMap);
  }

  private static IResult createFlow(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    EFlowTagType type = EFlowTagType.FLOW;
    String errMsg = "创建" + type.getDescription() + "失败";

    HFlow newFlow = new HFlow();
    EFlowHBUtil.setBasicProperty(newFlow, pMap);

    String code = newFlow.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newFlow.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    Session session = sessionWrapper.getSession();
    if (pMap.hasProperty("deptLevel")) {
      int l = pMap.getPropertyInt("deptLevel");
      newFlow.setDepartmentLevel(EFlow.getProperDepartmentLevel(l));
    }

    String todoLabel = pMap
      .getPropertyString("todoLabel");
    newFlow.setTodoLabel(todoLabel);

    String mainFormUN = pMap
      .getPropertyString("mainFormUN");
    if (StringUtil.isNotNullEmptyStr(mainFormUN)) {
      Object obj = EFlowHBUtil.queryIdByUN(session, EFlowTagType.FLOW, 
        mainFormUN);
      if (obj == null) {
        return GResult.newFailureResult("没有找到主表单").addErrMsgPreStr(errMsg);
      }
      newFlow.setMainForm((HForm)obj);
    }

    String mainCGUN = pMap.getPropertyString("mainCGUN");
    if (StringUtil.isNotNullEmptyStr(mainCGUN)) {
      Object obj = EFlowHBUtil.queryObjByUN(session, EFlowTagType.COMMENTGROUP, 
        mainCGUN);
      if (obj == null) {
        return GResult.newFailureResult("没有找到主意见组").addErrMsgPreStr(errMsg);
      }
      newFlow.setMainCommentGroup((HCommentGroup)obj);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }
    if (!pType.isTagCanBeMyChild(type)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建角色")
        .addErrMsgPreStr(errMsg);
    }
    errMsg = "在" + pun + "下创建" + type.getDescription() + code + "(" + name + 
      ")失败";

    Object obj = EFlowHBUtil.queryObjByUN(session, 
      EFlowHBUtil.getClassName(pType), pun);
    if (obj == null) {
      return SGResult.newFailureResult("没有找到父节点").addErrMsgPreStr(errMsg);
    }
    HApp app = (HApp)obj;

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(type));
    sb.append(" as obj where obj.app=:app");
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    Query q = session.createQuery(sb.toString());
    q.setEntity("app", app);
    q.setString("code", code);
    q.setString("name", name);
    q.setMaxResults(1);
    newObj = q.uniqueResult();
    if (newObj != null) {
      return SGResult.newFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    List<HGroup> groupList = null;
    List<HGroup> deptGroupList = null;
    boolean bool = pMap.getPropertyBool("subFlowFlag");
    newFlow.setSubflow(bool);

    String readerGroup = pMap
      .getPropertyString("readerGroup");
    if ((!newFlow.isSubflow()) && (StringUtil.isNotNullEmptyStr(readerGroup))) {
      String[] sa = 
        StringUtil.split(readerGroup, ";");
      int n = sa.length;
      groupList = new ArrayList();
      for (int i = 0; i < n; i++) {
        HGroup group = (HGroup)EFlowHBUtil.queryObjByUN(session, 
          EFlowTagType.GROUP, sa[i]);
        if (group == null) {
          return SGResult.newFailureResult("无法找到群组[" + sa[i] + "]");
        }
        groupList.add(group);
      }
    }

    readerGroup = pMap.getPropertyString("deptReaderGroup");
    if ((!newFlow.isSubflow()) && (StringUtil.isNotNullEmptyStr(readerGroup))) {
      String[] sa = 
        StringUtil.split(readerGroup, ";");
      int n = sa.length;
      deptGroupList = new ArrayList();
      for (int i = 0; i < n; i++) {
        HGroup group = (HGroup)EFlowHBUtil.queryObjByUN(session, 
          EFlowTagType.GROUP, sa[i]);
        if (group == null) {
          return SGResult.newFailureResult("无法找到群组[" + sa[i] + "]");
        }
        deptGroupList.add(group);
      }
    }

    newFlow.setApp(app);
    newFlow.resetUniqueName();

    if (!sessionWrapper.beginTransaction()) {
      return SGResult.newFailureResult("开始数据库事务失败");
    }
    String loader = pMap.getPropertyString("loaderScope");
    UserScopeInfo usInfo = ConditionTool.genUserScopeByFomattedString(loader);
    if (usInfo != null) {
      HBCenter.getBClobHandle().setBlob(session, newFlow, usInfo);
    }
    session.save(newFlow);

    HFlowVersion fv = new HFlowVersion();
    fv.setFlow(newFlow);
    fv.setCreateTime(DateTimeUtil.getNow());
    fv.setVersionInt(1);
    session.save(fv);
    session.flush();

    if ((groupList != null) || (deptGroupList != null)) {
      if (groupList != null) {
        for (HGroup g : groupList) {
          HFlowGroup fg = new HFlowGroup();
          fg.setFlow(newFlow);
          fg.setGroup(g);
          fg.setDeptFlag(false);
          session.save(fg);
        }
      }

      if (deptGroupList != null) {
        for (HGroup g : deptGroupList) {
          HFlowGroup fg = new HFlowGroup();
          fg.setFlow(newFlow);
          fg.setGroup(g);
          fg.setDeptFlag(true);
          session.save(fg);
        }
      }
    }

    sessionWrapper.commit();

    session.refresh(newFlow);
    EFlowCache.putFlowCache(EFlowServerByHB.genFlowCache(newFlow));

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newFlow.getUniqueName()));
  }

  private static IResult createSys(ESession es, HBSessionWrapper sessionWrapper, IEOperation operation, EPropertyMap pMap)
  {
    String errMsg = "创建系统失败";

    HSys newSys = new HSys();
    EFlowHBUtil.setBasicProperty(newSys, pMap);

    String code = newSys.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newSys.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    errMsg = "创建系统" + code + "(" + name + ")失败";

    Session session = sessionWrapper.getSession();
    GResult gr = GResult.newSuccessResult();

    HSys sys = null;
    String qStr = "from " + EFlowHBUtil.getClassName(EFlowTagType.SYS) + 
      " as org where org.code=:code or org.name=:name";
    try {
      Query q = session.createQuery(qStr);
      q.setString("code", code);
      q.setString("name", name);
      sys = (HSys)q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("验证代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (sys != null) {
      return gr.resetFailureResult("代码或名称冲突").addErrMsgPreStr(errMsg);
    }

    if (pMap.hasProperty("apiType")) {
      AppInterfaceType apiType = AppInterfaceType.valueOf(pMap
        .getPropertyString("apiType"));
      newSys.setApiType(apiType);
    }

    if (pMap.hasProperty("apiClass")) {
      newSys.setApiClassName(
        pMap.getPropertyString("apiClass"));
    }

    if (pMap.hasProperty("apiOACfg")) {
      newSys.setUseDiiopCfg(
        pMap.getPropertyBool("apiOACfg"));
    }

    if (pMap.hasProperty("wsUser")) {
      newSys.setWebserviceUser(
        pMap.getPropertyString("wsUser"));
    }

    if (pMap.hasProperty("wsPwd")) {
      newSys
        .setWebservicePwd(pMap.getPropertyString("wsPwd"));
    }

    if (pMap.hasProperty("wsUrl")) {
      newSys
        .setWebserviceUrl(pMap.getPropertyString("wsUrl"));
    }

    newSys.resetUniqueName();

    gr = HBUtil.saveObject_CacheEx(session, newSys);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr(errMsg);
      sessionWrapper.setNeedRollBack(true);
    }

    SysInfo sysInfo = newSys.toBasicInfo();
    EFlowCache.putSysInfo(sysInfo);

    return SGResult.newSuccessResult(newSys.getUniqueName());
  }

  private static GResult genUserCode()
  {
    return IdCenter.newId8();
  }

  private static IResult createPUser(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建兼职用户失败";

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    String actUserUN = op.getParameterString("actUserUN");
    if (StringUtil.isNullOrEmptyStr(actUserUN)) {
      return SGResult.newFailureResult("实际用户的UN为空").addErrMsgPreStr(errMsg);
    }

    HViewUser viewNewUser = new HViewUser();
    EFlowHBUtil.setBasicProperty(viewNewUser, pMap);

    String code = viewNewUser.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      GResult gr = genUserCode();
      if (gr.isFailure()) return gr.addErrMsgPreStr(errMsg);
      code = gr.getResultObjStr();
      viewNewUser.setCode(code);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.USER)) {
      return SGResult.newFailureResult("不能在该父节点下新建用户：" + pun).addErrMsgPreStr(
        errMsg);
    }

    errMsg = "创建〖" + actUserUN + "〗的兼职用户用户失败";

    Session session = sessionWrapper.getSession();

    HViewOrg org = null;
    HViewOrgUnit viewOrgOU = null;
    HViewOrgUnit viewPOrgUnit = null;
    String orgId = null;
    if (pType == EFlowTagType.O) {
      org = (HViewOrg)EFlowHBUtil.queryObjByUN(session, 
        HViewOrg.class.getName(), pun);

      if (org != null) {
        StringBuffer sb = new StringBuffer(100);
        sb.append("from ").append(HViewOrgUnit.class.getName()).append(
          " as obj");
        sb.append(" where obj.orgId='").append(org.getId()).append("'");
        sb.append(" and obj.code='").append("org")
          .append("'");
        viewOrgOU = (HViewOrgUnit)session.createQuery(sb.toString())
          .setMaxResults(1).uniqueResult();
      }
    } else {
      viewPOrgUnit = (HViewOrgUnit)EFlowHBUtil.queryObjByUN(session, 
        HViewOrgUnit.class.getName(), pun);
    }

    if ((org == null) && (viewPOrgUnit == null))
      return GResult.newFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    orgId = org == null ? viewPOrgUnit.getOrgId() : org.getId();

    Object obj = EFlowHBUtil.queryObjByUN(session, HViewUser.class.getName(), 
      actUserUN);
    if (obj == null) return SGResult.newFailureResult("没有找到真实用户");
    HViewUser viewActUser = (HViewUser)obj;

    String name = viewActUser.getName();
    viewNewUser.setName(name);
    viewNewUser.setAlias(viewActUser.getAlias());
    viewNewUser.setPassword(viewActUser.getPassword());
    viewNewUser.setActUser(viewActUser);

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(HViewUser.class.getName());
    if (viewPOrgUnit == null) {
      sb
        .append(" as obj where obj.orgId=:orgId and (obj.code=:code or obj.name=:name");
      sb.append(" and obj.orgUnitId is null)");
    } else {
      sb.append(" as obj where (obj.orgId=:orgId and obj.code=:code)").append(
        " or (obj.orgUnitId=:orgUnitId and obj.name=:name)");
    }

    Object newObj = null;
    Query q = session.createQuery(sb.toString());
    q.setString("orgId", orgId);
    if (viewPOrgUnit != null) {
      q.setString("orgUnitId", viewPOrgUnit.getId());
    }
    q.setString("code", code);
    q.setString("name", name);
    q.setMaxResults(1);
    newObj = q.uniqueResult();
    if (newObj != null) {
      return SGResult.newFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    viewNewUser.setOrgId(orgId);
    if (viewPOrgUnit == null) {
      viewNewUser.setOrg(org);
      viewNewUser.setOrgUnitId(viewOrgOU.getId());
      viewNewUser.setOrgUnitUN(viewOrgOU.getUniqueName());
    } else {
      viewNewUser.setOrgUnit(viewPOrgUnit);
      viewNewUser.setOrgUnitUN(viewPOrgUnit.getUniqueName());
    }
    viewNewUser.resetFullName();
    viewNewUser.resetUniqueName();

    UserUsualWordComment uwc = null;
    boolean setWordComment = false;
    if (pMap.hasProperty("usualWord")) {
      setWordComment = true;
      String[] sa = StringUtil.splitXMLorEMLLikeList(pMap
        .getPropertyString("usualWord"), "Word");
      uwc = new UserUsualWordComment();
      int n = ArrayUtil.getArraySize(sa);
      for (int i = 0; i < n; i++) {
        uwc.addWord(null, sa[i]);
      }
    }

    if (pMap.hasProperty("usualComment")) {
      setWordComment = true;
      String[] sa = StringUtil.splitXMLorEMLLikeList(pMap
        .getPropertyString("usualComment"), "Comment");
      if (uwc == null) uwc = new UserUsualWordComment();
      int n = ArrayUtil.getArraySize(sa);
      for (int i = 0; i < n; i++) {
        uwc.addComment(null, sa[i]);
      }
    }

    if (!sessionWrapper.beginTransaction())
      return SGResult.newFailureResult("开始数据库事务失败").addErrMsgPreStr(errMsg);
    if (setWordComment) {
      HBCenter.getBClobHandle().setBlob(session, viewNewUser, uwc);
    }
    session.save(viewNewUser);

    GResult gr = PFCenter.getUtil().add(viewNewUser.toDto());
    if (gr.isFailure()) return gr;
    sessionWrapper.commit();

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      viewNewUser.getUniqueName()));
  }

  private static IResult createUser(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建用户失败";

    HViewUser viewNewUser = new HViewUser();
    EFlowHBUtil.setBasicProperty(viewNewUser, pMap);

    String code = viewNewUser.getCode();

    if (StringUtil.isNullOrEmptyStr(code))
    {
      GResult gr = genUserCode();
      if ((gr.isFailure()) || (gr.isNullResult())) {
        return SGResult.newFailureResult("自动生成code出错").addErrMsgPreStr(errMsg);
      }
      code = gr.getResultObjStr();
      viewNewUser.setCode(code);
    }

    String name = viewNewUser.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    int typeInt = 0;
    if (op.hasParameter("userActionFromOtherApp")) {
      typeInt = UserActionType.NEW.ordinal;
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.USER)) {
      return SGResult.newFailureResult("不能在该父节点下新建用户：" + pun).addErrMsgPreStr(
        errMsg);
    }

    errMsg = "创建用户〖" + code + "(" + name + ")〗失败";
    Session session = sessionWrapper.getSession();

    HViewOrg org = null;
    HViewOrgUnit viewOrgOU = null;
    HViewOrgUnit viewPOrgUnit = null;
    String orgId = null;
    if (pType == EFlowTagType.O) {
      org = (HViewOrg)EFlowHBUtil.queryObjByUN(session, 
        HViewOrg.class.getName(), pun);

      if (org != null) {
        StringBuffer sb = new StringBuffer(100);
        sb.append("from ").append(HViewOrgUnit.class.getName()).append(
          " as obj");
        sb.append(" where obj.orgId='").append(org.getId()).append("'");
        sb.append(" and obj.code='").append("org")
          .append("'");
        viewOrgOU = (HViewOrgUnit)session.createQuery(sb.toString())
          .setMaxResults(1).uniqueResult();
      }
    } else {
      viewPOrgUnit = (HViewOrgUnit)EFlowHBUtil.queryObjByUN(session, 
        HViewOrgUnit.class.getName(), pun);
    }

    if ((org == null) && (viewPOrgUnit == null))
      return GResult.newFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    orgId = org == null ? viewPOrgUnit.getOrgId() : org.getId();

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(HViewUser.class.getName());
    if (viewPOrgUnit == null) {
      sb
        .append(" as obj where obj.orgId=:orgId and (obj.code=:code or obj.name=:name");
      sb.append(" and obj.orgUnitId is null)");
    } else {
      sb.append(" as obj where (obj.orgId=:orgId and obj.code=:code)").append(
        " or (obj.orgUnitId=:orgUnitId and obj.name=:name)");
    }

    Query q = session.createQuery(sb.toString());
    q.setString("orgId", orgId);
    if (viewPOrgUnit != null) {
      q.setString("orgUnitId", viewPOrgUnit.getId());
    }
    q.setString("code", code);
    q.setString("name", name);
    q.setMaxResults(1);

    if (q.list().size() > 0) {
      return GResult.newFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    viewNewUser.setActionTypeInt(typeInt);
    viewNewUser.setOrgId(orgId);
    if (viewPOrgUnit == null) {
      viewNewUser.setOrg(org);
      viewNewUser.setOrgUnitId(viewOrgOU.getId());
      viewNewUser.setOrgUnitUN(viewOrgOU.getUniqueName());
    } else {
      viewNewUser.setOrgUnit(viewPOrgUnit);
      viewNewUser.setOrgUnitUN(viewPOrgUnit.getUniqueName());
    }
    viewNewUser.resetFullName();
    viewNewUser.resetUniqueName();

    String pwd = pMap.getPropertyString("password");
    if (StringUtil.isNotNullEmptyStr(pwd)) viewNewUser.setPassword(pwd);

    String mailName = pMap.getPropertyString("mail");
    if (StringUtil.isNotNullEmptyStr(mailName)) {
      viewNewUser.setMailName(mailName);
    }
    String dName = pMap.getPropertyString("DName");
    if (StringUtil.isNotNullEmptyStr(dName)) viewNewUser.setDominoName(dName);

    String title = pMap.getPropertyString("title");
    if (StringUtil.isNotNullEmptyStr(title)) viewNewUser.setTitle(title);

    UserUsualWordComment uwc = null;
    boolean setWordComment = false;
    if (pMap.hasProperty("usualWord")) {
      setWordComment = true;
      String[] sa = StringUtil.splitXMLorEMLLikeList(pMap
        .getPropertyString("usualWord"), "Word");
      uwc = new UserUsualWordComment();
      int n = ArrayUtil.getArraySize(sa);
      for (int i = 0; i < n; i++) {
        uwc.addWord(null, sa[i]);
      }
    }

    if (pMap.hasProperty("usualComment")) {
      setWordComment = true;
      String[] sa = StringUtil.splitXMLorEMLLikeList(pMap
        .getPropertyString("usualComment"), "Comment");
      if (uwc == null) uwc = new UserUsualWordComment();
      int n = ArrayUtil.getArraySize(sa);
      for (int i = 0; i < n; i++) {
        uwc.addComment(null, sa[i]);
      }
    }

    if (pMap.hasProperty("mailOnly")) {
      viewNewUser.setMailOnly(
        pMap.getPropertyBool("mailOnly"));
    }
    String ownerSys = pMap.getPropertyString("ownerSys");
    viewNewUser.setOwnerSys(EFlowHBUtil.validateUserOwnerSys(ownerSys));

    if (!sessionWrapper.beginTransaction()) {
      return GResult.newFailureResult("开始数据库事务失败");
    }

    if (setWordComment) viewNewUser.setUsualWordComment(session, uwc);
    session.save(viewNewUser);

    GResult gr = PFCenter.getUtil().add(viewNewUser.toDto());
    if (gr.isFailure()) {
      sessionWrapper.setNeedRollBack(true);
      return gr;
    }

    sessionWrapper.commit();

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      viewNewUser.getUniqueName()));
  }

  private static IResult createFunGroup(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建功能组失败";

    HFunGroup newFunGroup = new HFunGroup();
    EFlowHBUtil.setBasicProperty(newFunGroup, pMap);

    String code = newFunGroup.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newFunGroup.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.FUNGROUP)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建角色")
        .addErrMsgPreStr(errMsg);
    }

    errMsg = "创建角色" + code + "(" + name + ")失败";

    Session session = sessionWrapper.getSession();
    GResult gr = GResult.newSuccessResult();

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, pType, pun);
    if (gr.isFailure()) {
      return gr.addErrMsgPreStr("查询父节点失败：" + pun).addErrMsgPreStr(errMsg);
    }

    if (gr.isNullResult()) {
      return gr.resetFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    }

    HApp app = (HApp)gr.getResultObject();

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(EFlowTagType.FUNGROUP));
    sb.append(" as obj where obj.app=:app");
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setEntity("app", app);
      q.setString("code", code);
      q.setString("name", name);
      newObj = q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (newObj != null) {
      return gr.resetFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    newFunGroup.setApp(app);
    newFunGroup.resetUniqueName();

    gr = HBUtil.saveObject_CacheEx(session, newFunGroup);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr(errMsg);
      sessionWrapper.setNeedRollBack(true);
    }

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newFunGroup.getUniqueName()));
  }

  private static IResult createGroup(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建群组失败";

    HGroup newGroup = new HGroup();
    EFlowHBUtil.setBasicProperty(newGroup, pMap);
    String code = newGroup.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newGroup.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    errMsg = "创建群组" + code + "(" + name + ")失败";

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.GROUP)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建群组")
        .addErrMsgPreStr(errMsg);
    }

    Session session = sessionWrapper.getSession();

    Object obj = EFlowHBUtil.queryObjByUN(session, pType, pun);
    if (obj == null) return GResult.newFailureResult("没有找到父节点：" + pun);

    HOrg org = null;
    HApp app = null;
    if (pType == EFlowTagType.O) {
      org = (HOrg)obj;
    } else {
      app = (HApp)obj;
      org = app.getOrg();
    }

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(EFlowTagType.GROUP));
    sb.append(" as obj where obj.org=:org");
    if (app == null)
      sb.append(" and obj.app is null");
    else {
      sb.append(" and obj.app=:app");
    }
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    Query q = session.createQuery(sb.toString());
    q.setEntity("org", org);
    if (app != null) {
      q.setEntity("app", app);
    }
    q.setString("code", code);
    q.setString("name", name);
    q.setMaxResults(1);
    newObj = q.uniqueResult();
    if (newObj != null) GResult.newFailureResult("代码或名称冲突!");

    newGroup.setOrg(org);
    newGroup.setApp(app);
    newGroup.resetUniqueName();

    if (!sessionWrapper.beginTransaction())
      GResult.newFailureResult("开始数据库事务失败!");
    boolean isNUGroup = pMap
      .getPropertyBool("isNoneUser");
    if (isNUGroup) {
      String str = pMap.getPropertyString("groupElement");
      GroupElementList gel = NoneUserGroupTool.toGroupElementList(str);
      Blob blob = HBUtil.objToBlob(gel);
      newGroup.setNoneUserGroupBlob(blob);
      newGroup.setNoneUserGroupFlag(true);
    } else {
      boolean isMailGroup = pMap
        .getPropertyBool("isMailGroup");
      newGroup.setMailGroupFlag(isMailGroup);
    }
    session.save(newGroup);

    //GResult gr = PFCenter.getUtil().add(newGroup.toDto());
    //if (gr.isFailure()) throw new EABnPRuntimeException(gr.getErrMsg());

    sessionWrapper.commit();

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newGroup.getUniqueName()));
  }

  private static IResult createFun(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建功能失败";

    HFun newFun = new HFun();
    EFlowHBUtil.setBasicProperty(newFun, pMap);

    String code = newFun.getCode();
    String name = newFun.getName();

    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.FUN)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建功能")
        .addErrMsgPreStr(errMsg);
    }

    errMsg = "创建功能" + code + "(" + name + ")失败";

    Session session = sessionWrapper.getSession();
    GResult gr = GResult.newSuccessResult();

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, pType, pun);
    if (gr.isFailure()) {
      return gr.addErrMsgPreStr("查询父节点失败：" + pun).addErrMsgPreStr(errMsg);
    }

    if (gr.isNullResult()) {
      return gr.resetFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    }

    HResource res = (HResource)gr.getResultObject();

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(EFlowTagType.FUN));
    sb.append(" as obj where obj.resource=:res");
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object obj = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setEntity("res", res);
      q.setString("code", code);
      q.setString("name", name);
      obj = q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (obj != null) {
      return gr.resetFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    newFun.setResource(res);
    newFun.setCode(code);
    newFun.setName(name);
    newFun.resetUniqueName();

    gr = HBUtil.saveObject_CacheEx(session, newFun);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr(errMsg);
      sessionWrapper.setNeedRollBack(true);
    }

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newFun.getUniqueName()));
  }

  private static IResult createResource(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建资源失败";

    HResource newRes = new HResource();
    EFlowHBUtil.setBasicProperty(newRes, pMap);

    String code = newRes.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newRes.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    EFlowTagType pType = EFlowTagUtil.getTypeFromName(pun);
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.RES)) {
      return SGResult.newFailureResult("不能在该父节点" + pun + "下新建资源")
        .addErrMsgPreStr(errMsg);
    }

    errMsg = "创建资源" + code + "(" + name + ")失败";

    Session session = sessionWrapper.getSession();
    GResult gr = GResult.newSuccessResult();

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, pType, pun);
    if (gr.isFailure()) {
      return gr.addErrMsgPreStr("查询父节点失败：" + pun).addErrMsgPreStr(errMsg);
    }

    if (gr.isNullResult()) {
      return gr.resetFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    }

    HApp app = null;
    HResource pRes = null;
    if (pType == EFlowTagType.APP) {
      app = (HApp)gr.getResultObject();
    } else {
      pRes = (HResource)gr.getResultObject();
      app = pRes.getApp();
    }

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(EFlowTagType.RES));
    sb.append(" as obj where obj.app=:app");
    if (pRes == null)
      sb.append(" and obj.parentRes is null");
    else {
      sb.append(" and obj.parentRes=:pRes");
    }
    sb.append(" and (obj.code=:code or obj.name=:name)");

    Object newObj = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setEntity("app", app);
      if (pRes != null) {
        q.setEntity("pRes", pRes);
      }
      q.setString("code", code);
      q.setString("name", name);
      newObj = (HResource)q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (newObj != null) {
      return gr.resetFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    newRes.setApp(app);
    newRes.setParentRes(pRes);
    newRes.resetUniqueName();

    gr = HBUtil.saveObject_CacheEx(session, newRes);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr(errMsg);
      sessionWrapper.setNeedRollBack(true);
    }

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newRes.getUniqueName()));
  }

  private static IResult createOrgUnit(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建组织单元失败";

    HViewOrgUnit viewNewOU = new HViewOrgUnit();
    EFlowHBUtil.setBasicProperty(viewNewOU, pMap);

    String code = viewNewOU.getCode();
    String name = viewNewOU.getName();

    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空").addErrMsgPreStr(errMsg);
    }

    EFlowTagType pType = EFlowTagType.valueOf(ETagUtil.getTypeStr(pun));
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.OU)) {
      return SGResult.newFailureResult("不能在该父节点下新建组织单元：" + pun)
        .addErrMsgPreStr(errMsg);
    }

    errMsg = "创建组织单元" + code + "(" + name + ")失败";

    GResult gr = GResult.newSuccessResult();
    Session session = sessionWrapper.getSession();

    HOrg org = null;
    HViewOrgUnit viewPOrgUnit = null;
    String orgId = null;
    if (pType == EFlowTagType.O)
      org = (HOrg)EFlowHBUtil.queryObjByUN(session, pType, pun);
    else {
      viewPOrgUnit = (HViewOrgUnit)EFlowHBUtil.queryObjByUN(session, 
        HViewOrgUnit.class.getName(), pun);
    }

    if ((org == null) && (viewPOrgUnit == null))
      return gr.resetFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    orgId = org == null ? viewPOrgUnit.getOrgId() : org.getId();

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(HViewOrgUnit.class.getName());
    sb.append(" as obj where obj.orgId=:orgId");
    if (viewPOrgUnit == null)
      sb.append(" and (obj.parentOrgUnitId is null or obj.parentOrgUnitId='')");
    else {
      sb.append(" and obj.parentOrgUnitId=:pOrgUnitId");
    }
    sb.append(" and (obj.code=:code or obj.name=:name)");

    HViewOrgUnit ou = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setString("orgId", orgId);
      if (viewPOrgUnit != null) {
        q.setString("pOrgUnitId", viewPOrgUnit.getId());
      }
      q.setString("code", code);
      q.setString("name", name);
      q.setMaxResults(1);
      ou = (HViewOrgUnit)q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (ou != null) {
      return gr.resetFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    if (org != null)
      viewNewOU.setOrg(org);
    else
      viewNewOU.setOrgId(orgId);
    viewNewOU.setParentOrgUnit(viewPOrgUnit);
    viewNewOU.resetFullName();
    viewNewOU.resetUniqueName();

    HViewOAServer viewOAServer = new HViewOAServer();
    DominoServerUtil.getHOAServer(pMap, viewOAServer);
    if (viewOAServer.validate()) {
      viewOAServer.setOrgId(null);
      viewOAServer.setOrgUnit(viewNewOU);
      viewNewOU.setOaServer(viewOAServer);
    } else {
      viewOAServer = null;
    }
    if (!sessionWrapper.beginTransaction()) {
      return gr.resetFailureResult("开始事务失败").addErrMsgPreStr(errMsg);
    }

    gr = PFCenter.getUtil().add(viewNewOU.toDto());
    if (gr.isFailure()) {
      return gr;
    }
    try
    {
      session.save(viewNewOU);
      if (viewNewOU.getOaServerSet() != null) {
        session.save(viewOAServer);
      }
      sessionWrapper.commit();
    } catch (Exception ex) {
      sessionWrapper.setNeedRollBack(true);
      return gr.resetFailureResult("保存对象失败", ex);
    }

    if (viewOAServer != null) {
      EFlowCache.putOAServer(viewNewOU.getUniqueName(), 
        viewOAServer.toBasicInfo());
    }
    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      viewNewOU.getUniqueName()));
  }

  private static IResult createApp(ESession es, HBSessionWrapper sessionWrapper, IEOperation op, EPropertyMap pMap)
  {
    String errMsg = "创建应用失败";

    String pun = op.getParameterString("parentUN");
    if (StringUtil.isNullOrEmptyStr(pun)) {
      return SGResult.newFailureResult("父节点为空");
    }
    EFlowTagType pType = EFlowTagUtil.getTypeFromName(pun);
    if (pType == null) {
      return SGResult.newFailureResult("错误的父节点形式：" + pun).addErrMsgPreStr(
        errMsg);
    }

    HApp newApp = new HApp();
    EFlowHBUtil.setBasicProperty(newApp, pMap);

    String code = newApp.getCode();
    String name = newApp.getName();
    String icName = pMap
      .getPropertyString("appInterface");
    newApp.setAppInterfaceClasssName(icName);

    String usualWord = pMap.getPropertyString("usualWord");
    newApp.setUsualWord(usualWord);

    newApp.setTodoPara(pMap.getPropertyString("todoPara"));

    String sysUN = null;
    if (pType != EFlowTagType.O)
    {
      if (StringUtil.isNullOrEmptyStr(code)) {
        return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
      }

      if (StringUtil.isNullOrEmptyStr(name)) {
        return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
      }

      errMsg = "在" + pun + "下创建应用" + code + "(" + name + ")失败";
    } else {
      sysUN = op.getParameterString("sysUN");
      if (StringUtil.isNullOrEmptyStr(sysUN)) {
        return SGResult.newFailureResult("系统为空").addErrMsgPreStr(errMsg);
      }
      if (EFlowTagType.SYS != EFlowTagUtil.getTypeFromName(sysUN)) {
        return SGResult.newFailureResult("系统参数错误").addErrMsgPreStr(errMsg);
      }

      code = ETagUtil.getCodeFromUN(sysUN);
      newApp.setCode(code);
      errMsg = "在" + pun + "下创建应用" + code + "失败";
    }

    if (!pType.isTagCanBeMyChild(EFlowTagType.APP)) {
      return SGResult.newFailureResult("不能在该父节点下新建应用：" + pun).addErrMsgPreStr(
        errMsg);
    }

    GResult gr = GResult.newSuccessResult();
    Session session = sessionWrapper.getSession();

    HSys sys = null;
    if (pType == EFlowTagType.O) {
      gr = EFlowHBUtil.queryObjByUN_CatchEx(session, EFlowTagType.SYS, sysUN);
      if (gr.isFailure()) {
        return gr.addErrMsgPreStr("查询系统失败：" + sysUN).addErrMsgPreStr(errMsg);
      }
      if (gr.isNullResult()) {
        return gr.resetFailureResult("系统" + sysUN + "不存在").addErrMsgPreStr(
          errMsg);
      }

      sys = (HSys)gr.getResultObject();
      name = sys.getName();
      newApp.setName(name);
    }

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, pType, pun);
    if (gr.isFailure()) {
      return gr.addErrMsgPreStr("查询父节点失败：" + pun).addErrMsgPreStr(errMsg);
    }

    if (gr.isNullResult()) {
      return gr.resetFailureResult("没有找到父节点：" + pun).addErrMsgPreStr(errMsg);
    }

    HOrg org = null;
    HApp pApp = null;
    HOrgUnit ou = null;
    if (pType == EFlowTagType.O) {
      org = (HOrg)gr.getResultObject();
    } else {
      pApp = (HApp)gr.getResultObject();
      org = pApp.getOrg();
      sys = pApp.getSys();

      String ouUN = pMap.getPropertyString("ouUN");
      if (StringUtil.isNotNullEmptyStr(ouUN)) {
        gr = EFlowHBUtil.queryObjByUN_CatchEx(session, EFlowTagType.OU, ouUN);
        ou = (HOrgUnit)gr.getResultObject();
      }
    }

    StringBuffer sb = new StringBuffer();
    sb.append("from ").append(EFlowHBUtil.getClassName(EFlowTagType.APP));
    sb.append(" as app where app.org=:org");
    if (pApp == null)
      sb.append(" and app.parentApp is null");
    else {
      sb.append(" and app.parentApp=:pApp");
    }
    sb.append(" and (app.code=:code or app.name=:name)");

    HApp app = null;
    try {
      Query q = session.createQuery(sb.toString());
      q.setEntity("org", org);
      if (pApp != null) {
        q.setEntity("pApp", pApp);
      }
      q.setString("code", code);
      q.setString("name", name);
      app = (HApp)q.uniqueResult();
    } catch (Exception ex) {
      return gr.resetFailureResult("检查代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
    }

    if (app != null) {
      return gr.resetFailureResult("代码、名称冲突").addErrMsgPreStr(errMsg);
    }

    String leaderGroupUN = pMap
      .getPropertyString("leaderGroup");
    if (StringUtil.isNotNullEmptyStr(leaderGroupUN)) {
      gr = EFlowHBUtil.queryObjByUN_CatchEx(session, EFlowTagType.GROUP, 
        leaderGroupUN);
      if (gr.isFailure())
        return gr.addErrMsgPreStr("查找单位领导群组『" + leaderGroupUN + "』失败");
      if (gr.isNullResult()) {
        return gr.resetFailureResult("没有找到组合代码为『" + leaderGroupUN + "』的群组");
      }
      newApp.setLeaderGroup((HGroup)gr.getResultObject());
    }

    newApp.setSys(sys);
    newApp.setOrg(org);
    if (ou != null) {
      newApp.setOrgUnit(ou);
    }

    newApp.setParentApp(pApp);
    newApp.resetUniqueName();

    if (pMap.hasProperty("deptLevel")) {
      int l = pMap.getPropertyInt("deptLevel");
      if ((l >= 0) && (l <= 4)) {
        newApp.setDepartmentLevel(l);
      }
    }
    if ((pMap.hasProperty("appInterface")) && 
      (pType == EFlowTagType.O)) {
      newApp.setAppInterfaceClasssName(
        pMap.getPropertyString("appInterface"));
    }

    HGroup admGroup = new HGroup();
    admGroup.setSysGroupFlag(true);
    String tagName = StringUtil.getLeftString("G=C:appAdm,N:应用管理者,SN:0;该组成员可以对本应用下的所有元素进行管理", ";");
    String des = StringUtil.getRightString("G=C:appAdm,N:应用管理者,SN:0;该组成员可以对本应用下的所有元素进行管理", ";");
    admGroup.setCode(EFlowConst.getCodeFromCfgStr(tagName));
    admGroup.setName(EFlowConst.getNameFromCfgStr(tagName));
    admGroup.setSequenceNum(EFlowConst.getSNFromCfgSTr(tagName));
    admGroup.setDescription(des);
    admGroup.setApp(newApp);
    admGroup.setOrg(org);
    admGroup.resetUniqueName();

    tagName = StringUtil.getLeftString("G=C:appCfg,N:应用配置者,SN:10;该组成员可以对本应用下的所有元素进行配置", ";");
    des = StringUtil.getRightString("G=C:appCfg,N:应用配置者,SN:10;该组成员可以对本应用下的所有元素进行配置", ";");
    HGroup cfgGroup = new HGroup();
    cfgGroup.setSysGroupFlag(true);
    cfgGroup.setCode(EFlowConst.getCodeFromCfgStr(tagName));
    cfgGroup.setName(EFlowConst.getNameFromCfgStr(tagName));
    cfgGroup.setSequenceNum(EFlowConst.getSNFromCfgSTr(tagName));
    cfgGroup.setDescription(des);
    cfgGroup.setApp(newApp);
    cfgGroup.setOrg(org);
    cfgGroup.resetUniqueName();

    if (!sessionWrapper.beginTransaction())
      return gr.resetFailureResult("开始事务失败").addErrMsgPreStr(errMsg);
    try
    {
      session.save(newApp);
      session.save(admGroup);
      session.save(cfgGroup);

      sessionWrapper.commit();
    } catch (Exception ex) {
      return gr.resetFailureResult("保存对象失败", ex);
    }

    AppInfo appInfo = newApp.toBasicInfo();
    EFlowCache.putAppInfo(appInfo);

    return SGResult.newSuccessResult(StringUtil.toXMLLikeString("UniqueName", 
      newApp.getUniqueName()));
  }

  private static IResult createOrg(ESession es, HBSessionWrapper sessionWrapper, IEOperation operation, EPropertyMap pMap)
  {
    String errMsg = "创建组织失败";

    HOrg newOrg = new HOrg();
    EFlowHBUtil.setBasicProperty(newOrg, pMap);

    String code = newOrg.getCode();
    if (StringUtil.isNullOrEmptyStr(code)) {
      return SGResult.newFailureResult("代码为空").addErrMsgPreStr(errMsg);
    }

    String name = newOrg.getName();
    if (StringUtil.isNullOrEmptyStr(name)) {
      return SGResult.newFailureResult("名称为空").addErrMsgPreStr(errMsg);
    }

    newOrg.resetUniqueName();
    newOrg.resetFullName();
    errMsg = "创建组织" + newOrg.toString() + ")失败";

    Session session = sessionWrapper.getSession();
    GResult gr = GResult.newSuccessResult();

    Object obj = null;
    String qStr = "from " + EFlowHBUtil.getClassName(EFlowTagType.O) + 
      " as org where org.code=:code or org.name=:name";
    try {
      Query q = session.createQuery(qStr);
      q.setString("code", code);
      q.setString("name", name);
      obj = q.uniqueResult();
    } catch (Exception ex) {
      gr.resetFailureResult("验证代码、名称冲突失败", ex).addErrMsgPreStr(errMsg);
      return gr;
    }
    if (obj != null) {
      gr.resetFailureResult("代码或名称冲突").addErrMsgPreStr(errMsg);
      return gr;
    }

    gr = EFlowHBUtil.queryObjByUN_CatchEx(session, EFlowTagType.SYS, 
      EFlowConst.EABNP_SYS_UN);
    if (gr.isFailure()) {
      gr.addErrMsgPreStr("查找〖" + EFlowConst.EABNP_SYS_UN + "〗失败");
      return gr;
    }
    if (gr.isNullResult()) {
      gr.resetFailureResult("无法找到〖" + EFlowConst.EABNP_SYS_UN + "〗");
      return gr;
    }

    HSys eSys = (HSys)gr.getResultObject();

    HApp orgApp = new HApp();
    orgApp.setCode(eSys.getCode());
    orgApp.setName(eSys.getName());
    orgApp.setOrg(newOrg);
    orgApp.setSys(eSys);
    orgApp.setSequenceNum(100);
    orgApp.resetUniqueName();

    HViewOrgUnit viewMyselfOU = newOrg.createMyselfOU();

    HGroup admGroup = new HGroup();
    admGroup.setSysGroupFlag(true);
    String tagName = StringUtil.getLeftString("G=C:adm,N:本组织管理者,SN:0;该组成员可以对本组织下的机构、用户、群组进行管理", ";");
    String des = StringUtil.getRightString("G=C:adm,N:本组织管理者,SN:0;该组成员可以对本组织下的机构、用户、群组进行管理", ";");
    admGroup.setCode(EFlowConst.getCodeFromCfgStr(tagName));
    admGroup.setName(EFlowConst.getNameFromCfgStr(tagName));
    admGroup.setSequenceNum(EFlowConst.getSNFromCfgSTr(tagName));
    admGroup.setDescription(des);
    admGroup.setApp(orgApp);
    admGroup.setOrg(newOrg);
    admGroup.resetUniqueName();

    HGroup ou1Group = new HGroup();
    ou1Group.setSysGroupFlag(true);
    tagName = StringUtil.getLeftString("G=C:ou1Adm,N:本OU1管理者,SN:0;该组成员可以对本OU1下的机构、用户、群组进行管理", ";");
    des = StringUtil.getRightString("G=C:ou1Adm,N:本OU1管理者,SN:0;该组成员可以对本OU1下的机构、用户、群组进行管理", ";");
    ou1Group.setCode(EFlowConst.getCodeFromCfgStr(tagName));
    ou1Group.setName(EFlowConst.getNameFromCfgStr(tagName));
    ou1Group.setSequenceNum(EFlowConst.getSNFromCfgSTr(tagName));
    ou1Group.setDescription(des);
    ou1Group.setApp(orgApp);
    ou1Group.setOrg(newOrg);
    ou1Group.resetUniqueName();

    HGroup ou2Group = new HGroup();
    ou2Group.setSysGroupFlag(true);
    tagName = StringUtil.getLeftString("G=C:ou2Adm,N:本OU2管理者,SN:0;该组成员可以对本OU2下的机构、用户、群组进行管理", ";");
    des = StringUtil.getRightString("G=C:ou2Adm,N:本OU2管理者,SN:0;该组成员可以对本OU2下的机构、用户、群组进行管理", ";");
    ou2Group.setCode(EFlowConst.getCodeFromCfgStr(tagName));
    ou2Group.setName(EFlowConst.getNameFromCfgStr(tagName));
    ou2Group.setSequenceNum(EFlowConst.getSNFromCfgSTr(tagName));
    ou2Group.setApp(orgApp);
    ou2Group.setOrg(newOrg);
    ou2Group.resetUniqueName();

    HOAServer oaServer = new HOAServer();
    DominoServerUtil.getHOAServer(pMap, oaServer);
    if (oaServer.validate()) {
      oaServer.setOrg(newOrg);
      newOrg.setOaServer(oaServer);
    } else {
      oaServer = null;
    }

    gr = PFCenter.getUtil().add(newOrg);
    if (gr.isFailure()) {
      return gr;
    }

    if (!sessionWrapper.beginTransaction()) {
      return gr.resetFailureResult("开始事务失败").addErrMsgPreStr(errMsg);
    }
    try
    {
      session.save(newOrg);
      if (!"special".equals(code)) {
        session.save(orgApp);
        viewMyselfOU.setOrgId(newOrg.getId());
        session.save(viewMyselfOU);
        session.save(admGroup);
        session.save(ou1Group);
        session.save(ou2Group);
        if (newOrg.getOaServerSet() != null) {
          session.save(oaServer);
        }
      }

      sessionWrapper.commit();
    } catch (Exception ex) {
      return gr.resetFailureResult("保存对象失败", ex);
    }

    if (oaServer != null)
      EFlowCache.putOAServer(newOrg.getUniqueName(), oaServer.toBasicInfo());
    return gr.resetSuccessResult("");
  }
}